package com.hj.system.base.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.WebSocketConnectionManager;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.TextWebSocketHandler;

public class WebSocketClient {
    Logger logger = LoggerFactory.getLogger(WebSocketClient.class);
    private static final String WS_URI = "ws://127.0.0.1:8181/add";

    public static void main(String[] args) throws  InterruptedException {
        StandardWebSocketClient client = new StandardWebSocketClient();
        WebSocketConnectionManager manager = new WebSocketConnectionManager(client, new MyHandler(), WS_URI);
        manager.start();
//        Thread.sleep(10000);
    }

    private static class MyHandler extends TextWebSocketHandler {
        @Override
        public void afterConnectionEstablished(WebSocketSession session) throws Exception {
            System.out.println("connected...........");
            //172.16.201.211
            String pd="{\"Code\":\"InventoryReal\",\"Ip\":\"172.16.201.211\",\"Antennas\":\"0，1\"}";
            //"{\"code\":\"LockOpen\",\"IP\":\"172.16.201.9\",\"lockNum\":\"1\",\"lattice\":\"2\"}"
            session.sendMessage(new TextMessage(pd));
            super.afterConnectionEstablished(session);
        }

        @Override
        protected void handleTextMessage(WebSocketSession session, TextMessage message)
                throws Exception {
            System.out.println("receive: " + message.getPayload());
//            super.handleTextMessage(session, message);
        }
    }
}